package com.example.demo.Controller;

import com.example.demo.Entity.CustomerEntity;
import com.example.demo.Entity.ResultEntity;
import com.example.demo.Service.CustomerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/customer")
@CrossOrigin
public class CustomerController {
    @Resource
    CustomerService customerService;

    @RequestMapping(value = "/getCustomerInfo")
    public ResultEntity GetCustomer() {
        List<CustomerEntity> customerEntity;
        customerEntity = customerService.get();

        ResultEntity res = new ResultEntity();
        res.setCode(20000);
        res.setData(customerEntity);
        return res;
    }

    @RequestMapping(value = "/getCustomerInfoByAccounts")
    public ResultEntity GetCustomerInfoByAccounts(@RequestParam("accounts") String accounts) {
        CustomerEntity customerEntity;
        customerEntity = customerService.getByAccounts(accounts);

        ResultEntity res = new ResultEntity();
        res.setCode(20000);
        res.setData(customerEntity);
        return res;
    }

    @RequestMapping(value = "/updateCustomerInfoByAccounts")
    public ResultEntity UpdateCustomerInfoByAccounts(@RequestBody CustomerEntity data) {
        int customerEntity;
        customerEntity = customerService.updateByAccounts(data.getAccounts(),data.getUsername(),data.getPassword(),data.getAvatar(),data.getEmail(),data.getGender(),data.getTelephone());

        ResultEntity res = new ResultEntity();
        res.setCode(20000);
        res.setData(customerEntity);
        return res;
    }

    @RequestMapping(value = "/addCustomer")
    public ResultEntity AddCustomer(@RequestBody CustomerEntity data) {
        int customerEntity;
        customerEntity = customerService.addCustomer(data.getAccounts(),data.getUsername(),data.getPassword(),data.getAvatar(),data.getEmail(),data.getGender(),data.getTelephone());

        ResultEntity res = new ResultEntity();
        res.setCode(20000);
        res.setData(customerEntity);
        return res;
    }

    @RequestMapping(value = "/deleteCustomer")
    public ResultEntity DeleteCustomer(@RequestParam("accounts") String accounts) {
        int customerEntity;
        customerEntity = customerService.deleteCustomer(accounts);

        ResultEntity res = new ResultEntity();
        res.setCode(20000);
        res.setData(customerEntity);
        return res;
    }
}
